package com.bykj.taketurns.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewConfigurationCompat;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

import com.bykj.taketurns.R;
import com.bykj.taketurns.activity.PersonalActivity;
import com.bykj.taketurns.activity.WriteScreenActivity;

/**
 * Created by Administrator on 2017/8/17.
 * 本类自定义的nestedScrollview，解决与viewpager的上下滑动冲突
 */

public class ReadNestedScroll extends NestedScrollView  {
    private boolean isScrolledToTop = true;// 初始化的时候设置一下值
    private boolean isScrolledToBottom = false;
    private ISmartScrollChangedListener mSmartScrollChangedListener;
    private int mLastY;
    private int mLastX;
    private int dy;
    private boolean isTop;
    private boolean isBottom;
    private int x2;
    private int y2;
    private int dx;
    private Activity mContext;
    private int mTouchSlop;

    public ReadNestedScroll(Context context) {
       this(context,null);
    }

    public ReadNestedScroll(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public ReadNestedScroll(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext= (Activity) context;
        ViewConfiguration configuration = ViewConfiguration.get(context);
        // 获取TouchSlop值
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
    }



    /** 定义监听接口 */
    public interface ISmartScrollChangedListener {
        void onScrolledToBottom();
        void onScrolledToTop();
    }
    public void setScanScrollChangedListener(ISmartScrollChangedListener smartScrollChangedListener) {
        mSmartScrollChangedListener = smartScrollChangedListener;
    }
    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        if (scrollY == 0) {
            isScrolledToTop = clampedY;
            isScrolledToBottom = false;
        } else {
            isScrolledToTop = false;
            isScrolledToBottom = clampedY;
        }
        notifyScrollChangedListeners();
    }
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (android.os.Build.VERSION.SDK_INT < 9) {  // API 9及之后走onOverScrolled方法监听
            if (getScrollY() == 0) {    // 小心踩坑1: 这里不能是getScrollY() <= 0
                isScrolledToTop = true;
                isScrolledToBottom = false;
            } else if (getScrollY() + getHeight() - getPaddingTop() - getPaddingBottom() == getChildAt(0).getHeight()) {
                // 小心踩坑2: 这里不能是 >=
                // 小心踩坑3（可能忽视的细节2）：这里最容易忽视的就是ScrollView上下的padding　
                isScrolledToBottom = true;
                isScrolledToTop = false;
            } else {
                isScrolledToTop = false;
                isScrolledToBottom = false;
            }
            notifyScrollChangedListeners();
        }
    }
    private void notifyScrollChangedListeners() {
        if (isScrolledToTop) {
            if (mSmartScrollChangedListener != null) {
                mSmartScrollChangedListener.onScrolledToTop();
            }
        } else if (isScrolledToBottom) {
            if (mSmartScrollChangedListener != null) {
                mSmartScrollChangedListener.onScrolledToBottom();
            }
        }
    }

    public boolean isScrolledToTop() {
        return isScrolledToTop;
    }

    public boolean isScrolledToBottom() {
        return isScrolledToBottom;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        // 第一步，记录手指在view的坐标。

        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                int x = (int) ev.getRawX();
                int y = (int) ev.getRawY();
                // 更新手指此时的坐标。
                mLastX = x;
                mLastY = y;
                return true;
            }
            case MotionEvent.ACTION_MOVE: {
                int x = (int) ev.getX();
                int y = (int) ev.getRawY();
                // 计算手指此时的坐标和上次的坐标滑动的距离。
                dy = y - mLastY;
                dx=x-mLastX;
                if(Math.abs(dx)>Math.abs(dy)){
                    break;
                }
            }
            case MotionEvent.ACTION_UP:
                final float upX = ev.getX();
                final float upY= ev.getY();
                final float dxup = upX - mLastX;
                final float dyup = upY - mLastY;
                y2 = (int) ev.getRawY();
                if(Math.abs(dxup)>mTouchSlop){
                    if(Math.abs(dxup)>Math.abs(dyup)) {
                        if (dxup > 0) {
                            Intent intent = new Intent(mContext, WriteScreenActivity.class);
                            mContext.startActivity(intent);
                            mContext.overridePendingTransition(R.anim.trans_left_in, R.anim.trans_right_out);
                            return true;
                        } else {
                            Intent intent = new Intent(mContext, PersonalActivity.class);
                            mContext.startActivity(intent);
                            mContext.overridePendingTransition(R.anim.tran_in, R.anim.tran_out);
                            return true;
                        }
                    }
                }
                if(mLastY - y2 > 50) {
                    isTop=true;
                    isBottom=false;
                } else if(y2 - mLastY > 50) {
                    isBottom=true;
                    isTop=false;
                }
                if(isScrolledToBottom()&&isTop){
                    dy=0;
                    return false;
                }
                if(isScrolledToTop()&&dy>0&&isBottom){
                    dy=0;
                    return false;
                }
            case MotionEvent.ACTION_CANCEL: {
                final float cX = ev.getX();
                final float cY= ev.getY();
                final float dx = cX - mLastX;
                final float dy = cY - mLastY;
                if(Math.abs(dx)>mTouchSlop){
                    if(Math.abs(dx)>Math.abs(dy)) {
                        if (dx > 0) {
                            Intent intent = new Intent(mContext, WriteScreenActivity.class);
                            mContext.startActivity(intent);
                            mContext.overridePendingTransition(R.anim.trans_left_in, R.anim.trans_right_out);
                        } else {
                            Intent intent = new Intent(mContext, PersonalActivity.class);
                            mContext.startActivity(intent);
                            mContext.overridePendingTransition(R.anim.tran_in, R.anim.tran_out);
                        }
                        return true;
                    }else {
                       if(isScrolledToBottom()||isScrolledToTop()){
                           return false;
                       }
                    }
                }
            }

        }

        return super.onTouchEvent(ev);
    }
}
